package role

import (
	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	zhtranslations "github.com/go-playground/validator/v10/translations/zh"
	"jinrigaoping/api/platform/internal/logic/sys/role"
	"jinrigaoping/api/platform/internal/svc"
	"jinrigaoping/api/platform/internal/types"
	"jinrigaoping/pkg/xcode"
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"
)

func RoleDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.SysRoleDeleteReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		// 中文翻译器
		uni := ut.New(zh.New())
		trans, _ := uni.GetTranslator("zh")
		validate := validator.New()

		err := zhtranslations.RegisterDefaultTranslations(validate, trans)

		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		if err := validate.StructCtx(r.Context(), req); err != nil {
			for _, err := range err.(validator.ValidationErrors) {
				httpx.ErrorCtx(r.Context(), w, err)
				return
			}
		}

		l := role.NewRoleDeleteLogic(r.Context(), svcCtx)
		resp, err := l.RoleDelete(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, xcode.OkJson(resp))
		}
	}
}
